package com.mxy.myapplication.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.mxy.myapplication.R;
import com.mxy.myapplication.adapter.AdapterForRv_Linear;
import com.mxy.myapplication.pojo.Pojo1;
import com.mxy.myapplication.util.LinearLayoutColorDivider;

import java.util.ArrayList;

/**
 * ViewPager上的碎片 线性布局
 */

public class FragmentOfViewPager_Linear extends Fragment {

    ArrayList<Pojo1> data = new ArrayList<Pojo1>();
    TextView stickyheaderview;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fragment_linearlayout, container, false);
        stickyheaderview = (TextView) v.findViewById(R.id.stickyheaderview);
        RecyclerView rv = (RecyclerView) v.findViewById(R.id.rv);

        LinearLayoutManager llm = new LinearLayoutManager(getActivity());
        llm.setOrientation(LinearLayoutManager.VERTICAL);

        rv.setLayoutManager(llm);
        rv.setItemAnimator(new DefaultItemAnimator());
        rv.addItemDecoration(new LinearLayoutColorDivider(getActivity().getResources(),
                android.R.color.transparent, R.dimen.dip0, LinearLayoutManager.VERTICAL));
        rv.addOnScrollListener(new OnscrollingListener());

        initData();
        AdapterForRv_Linear adapter = new AdapterForRv_Linear(this.getActivity(), data);
        rv.setAdapter(adapter);

        return v;
    }

    public void initData() {
        data.clear();
        data.add(new Pojo1(1, R.drawable.jj1, "进击的巨人"));
        data.add(new Pojo1(0, R.drawable.jj2, "进击的巨人"));
        data.add(new Pojo1(0, R.drawable.jj3, "进击的巨人"));
        data.add(new Pojo1(1, R.drawable.bleach1, "死神"));
        data.add(new Pojo1(0, R.drawable.bleach2, "死神"));
        data.add(new Pojo1(0, R.drawable.bleach3, "死神"));
        data.add(new Pojo1(1, R.drawable.guanlan1, "灌篮高手"));
        data.add(new Pojo1(0, R.drawable.guanlan2, "灌篮高手"));
        data.add(new Pojo1(0, R.drawable.guanlan3, "灌篮高手"));
    }

    /**
     * Rv滑动监听事件
     */
    private class OnscrollingListener extends RecyclerView.OnScrollListener {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);

            //实时变更stickyHeaderView的标签内容
            View v = recyclerView.findChildViewUnder(0, 0);//获取顶部第一个item的View
            if (v != null) {
                stickyheaderview.setText(v.getContentDescription());
            }

            //顶部的stickyHeaderView与item的stickyHeaderView接触时，顶部的stickyHeaderView向上移动效果
            View itemStickyHeaderView = recyclerView.findChildViewUnder(0,
                    stickyheaderview.getMeasuredHeight());
            if (itemStickyHeaderView != null) {
                int stickyheadertype = (Integer) itemStickyHeaderView.getTag();
                if (stickyheadertype == 1) {//该类型第一个item
                    if (itemStickyHeaderView.getTop() > 0) {
                        stickyheaderview.setTranslationY(itemStickyHeaderView.getTop() -
                                stickyheaderview.getMeasuredHeight());
                    } else {
                        stickyheaderview.setTranslationY(0);
                    }
                } else {//一般item
                    stickyheaderview.setTranslationY(0);
                }
            }
        }
    }
}